/* ====================================================================
 * Copyright (C) 2012 - Deniz Sezen
 * ====================================================================
 * File: dx_augsystem.cpp
 * Purpose: This file contains function implementations to interact
 *  with the augmentation system in DX:HR.
 * ====================================================================
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/************************************************************************/
/* Includes                                                             */
/************************************************************************/

/* Operating System includes. */
#include <windows.h>
#include <stdio.h>

/* dxhr-sdk includes. */
#include "dx_augsystem.h"
#include "dx_scanner.h"
#include "dx_hook.h"

/************************************************************************/
/* Globals.                                                             */
/************************************************************************/
extern FILE* g_pLogFile;
AugmentationSystem* g_pAugmentationSystem = NULL;

/************************************************************************/
/* Hook for augsystem constructor.                                      */
/************************************************************************/
CFunctionHook* g_pAugSystemHook = NULL;
void* __stdcall hook_AugmentationSystem( void )
{
	// Get object pointer.
	__asm mov g_pAugmentationSystem, ecx;

	// Save off registers and do stuff here.
	__asm pushad;
	fprintf(g_pLogFile, "AugmentationSystem hook called!\n");
	fflush(g_pLogFile);
	__asm popad;

	// Call the original function as intended.
	typedef void* (__thiscall *AugSysFn)( void );
	AugSysFn fn = (AugSysFn)g_pAugSystemHook->GetTrampoline();
	return fn();
}

/************************************************************************/
/* Initializes the augmentation system.                                 */
/************************************************************************/
void Init_AugSystem( void )
{
	// Get DLL Base.
	void* Base = GetModuleHandle( NULL );

	// Get address of augsys constructor.
	void* pFnAugSys = g_SigMngr.ResolveSig(Base, ENERGYDATA_SIG, ENERGYDATA_SIG_LEN);
	fprintf(g_pLogFile, "Found AugmentationSystem constructor at %p..\n", pFnAugSys);

	// Can't continue.
	if( !pFnAugSys ) {
		return;
	}

	// Hook the constructor.
	g_pAugSystemHook = new CFunctionHook((unsigned char*)pFnAugSys, &hook_AugmentationSystem);

	if( !g_pAugSystemHook->DoPatch() ) {
		fprintf(g_pLogFile, "Could not hook the augmentation system.\n");
		return;
	}

	fprintf(g_pLogFile, "AugmentationSystem functions hooked successfully.\n");
	fflush(g_pLogFile);
}